package com.studio49.exmple.controller.web.sys;

import com.github.pagehelper.PageHelper;
import com.studio49.exmple.common.constant.ForwardConstant;
import com.studio49.exmple.common.constant.SysConstant;
import com.studio49.exmple.common.util.MD5Utils;
import com.studio49.exmple.core.controller.BaseController;
import com.studio49.exmple.core.entity.pub.PageView;
import com.studio49.exmple.entity.web.sys.User;
import com.studio49.exmple.mapper.web.sys.UserMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP用户管理
 */
@Controller
@RequestMapping("/sys/appUser")
public class AppUserManageController extends BaseController {

	private static final Logger LOG = LogManager.getLogger(AppUserManageController.class.getName());

	@Autowired // TODO: 2019/4/29
	private UserMapper userMapper;

	/**
	 * 列表页
	 * @return
	 */
	@RequestMapping("/list")
	public String list(){
//		return "sys/appUser/list";
		return ForwardConstant.SYSTEM_H + ForwardConstant.APP_USER + ForwardConstant.LIST;
	}
	
	/**
	 * 分页查询
	 * @param keyWords
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/findByPage")
	@ResponseBody
	public PageView findByPage(String keyWords, Integer pageNow, Integer pageSize){
		if(pageNow == null) {
			pageNow = 1;
		}
		PageHelper.startPage(pageNow, pageSize);
		Map params = new HashMap();
		params.put("keyWords", keyWords);
		List<User> list = userMapper.findAppUser(params);

		PageView pageView = new PageView(list);
		return pageView;
	}

	@RequestMapping({"/updateEnable"})
	@ResponseBody
	public String updateEnable(Long id, Short isEnable)
	{
		try {
			User user = new User();
			user.setId(id);
			user.setIsEnable(isEnable);
			this.userMapper.updateByPrimaryKeySelective(user);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("更新用户状态出错！", e);
			return SysConstant.ERROR;
		}
		return SysConstant.SUCCESS;
	}

	@RequestMapping({"/resetPasswd"})
	@ResponseBody
	public String resetPasswd(Long id)
	{
		try {
			User user = new User();
			user.setId(id);
			user.setPassword(MD5Utils.md5("123456"));
			this.userMapper.updateByPrimaryKeySelective(user);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("重置用户密码出错！", e);
			return SysConstant.ERROR;
		}
		return SysConstant.SUCCESS;
	}


}
